'use strict';

if (process.env.NODE_ENV === undefined) {
  process.env.NODE_ENV = 'development';
}

const Koa = require('koa');
const kcors = require('kcors');
const KoaBody = require('koa-body');
const session = require('koa-session');

const app = new Koa();

const PORT = process.env.PORT || 1337;

const admin = require('./api/admin');
const article = require('./api/article');
const feedback = require('./api/feedback');
const system = require('./api/system');
const auth = require('./api/auth');

const CONFIG = {
  key: 'koa:sess', /** (string) cookie key (default is koa:sess) */
  /** (number || 'session') maxAge in ms (default is 1 days) */
  /** 'session' will result in a cookie that expires when session/browser is closed */
  /** Warning: If a session cookie is stolen, this cookie will never expire */
  maxAge: 86400000,
  overwrite: true, /** (boolean) can overwrite or not (default true) */
  httpOnly: true, /** (boolean) httpOnly or not (default true) */
  signed: false, /** (boolean) signed or not (default true) */
  rolling: false, /** (boolean) Force a session identifier cookie to be set on every response. The expiration is reset to the original maxAge, resetting the expiration countdown. default is false **/
};

app.use(KoaBody());
app.use(kcors());
app.use(session(CONFIG, app));
app.use(auth.routes());
app.use(admin.routes());
app.use(article.routes());
app.use(feedback.routes());
app.use(system.routes());

app.use(async (ctx, next) => {
  ctx.body = 'oops.';
});

app.listen(PORT, () => {
  console.log(`http://localhost:${PORT}`);
});
